package com.dreamwhale.accelerator.manage;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamwhale.accelerator.common.DataResult;
import com.dreamwhale.accelerator.entity.AlManage;
import com.dreamwhale.accelerator.entity.AlManageJurisdiction;
import com.dreamwhale.accelerator.mapper.AlManageJurisdictionMapper;
import com.dreamwhale.accelerator.mapper.AlManageMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/backstage")
public class ManageLoginController {

      @Resource
      private AlManageMapper alManageMapper;

      @Resource
      private AlManageJurisdictionMapper alManageJurisdictionMapper;

    /**
     *后台登录
     */
    @RequestMapping("/selectAlManage")
    @ResponseBody
    public DataResult selectAlManage(HttpServletRequest request, HttpServletResponse response,
                                     AlManage alManage) {
        try {
            QueryWrapper<AlManage> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("manage_account",alManage.getManageAccount());
            queryWrapper.eq("manage_password",alManage.getManagePassword());
            AlManage list= alManageMapper.selectOne(queryWrapper);
            if(list!=null){
                HttpSession session = request.getSession();
                session.setAttribute("user", list);
                request.setAttribute("user",list);
                session.setAttribute("mjIsAdmin", list.getManageType());
                request.setAttribute("user", list.getManageType());
                return  DataResult.ok();
            }else{
                return  DataResult.build(300,"账号不存在或密码错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return  DataResult.build(300,"登录失败,请联系管理员");
        }

    }

    /**
     *读取侧边栏
     * @return
     */
    @RequestMapping("/selectAlManageJurisdiction")
    public DataResult selectAlManageJurisdiction(HttpServletRequest request, HttpServletResponse response,HttpSession session) {

        QueryWrapper<AlManageJurisdiction> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mj_is_admin",session.getAttribute("mjIsAdmin")).eq("mj_is_del",0);
        List<AlManageJurisdiction> list =alManageJurisdictionMapper.selectList(queryWrapper);
        return DataResult.ok(list);
    }

}
